package bb.lanxing.util.ui;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TabWidget;
import android.widget.TextView;
import androidx.appcompat.widget.AppCompatTextView;
import bb.lanxing.manager.SharedManager;
//import bb.lanxing.util.jsbridge.BridgeUtil;

public class BadgeView extends AppCompatTextView {
    private boolean autoHide;
    private boolean mHideOnNull;
    private OnHideListener onHideListener;

    public interface OnHideListener {
        void onHide();
    }

    public BadgeView(Context context) {
        this(context, null);
    }

    public BadgeView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, android.R.attr.textViewStyle);
    }

    public BadgeView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.mHideOnNull = true;
        this.autoHide = false;
        init();
    }

    private void init() {
        if (!(getLayoutParams() instanceof FrameLayout.LayoutParams)) {
            setLayoutParams(new FrameLayout.LayoutParams(-2, -2, 53));
        }
        setBackground(5, Color.parseColor("#FE4545"));
        setGravity(Gravity.CENTER);
    }

    public void setBackground(int i, int i2) {
        float dip2Px = dip2Px(i);
        ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(new float[]{dip2Px, dip2Px, dip2Px, dip2Px, dip2Px, dip2Px, dip2Px, dip2Px}, null, null));
        shapeDrawable.getPaint().setColor(i2);
        setBackground(shapeDrawable);
    }

    public boolean isHideOnNull() {
        return this.mHideOnNull;
    }

    public BadgeView setHideOnNull(boolean z) {
        this.mHideOnNull = z;
        setText(getText());
        return this;
    }

    public boolean isAutoHide() {
        return this.autoHide;
    }

    public BadgeView setAutoHide(boolean z) {
        this.autoHide = z;
        return this;
    }

    @Override
    public void setText(CharSequence charSequence, TextView.BufferType bufferType) {
        if (isHideOnNull() && (charSequence == null || charSequence.toString().equalsIgnoreCase("0"))) {
            setVisibility(View.GONE);
        } else {
            setVisibility(View.VISIBLE);
            if (charSequence == null || charSequence.toString().isEmpty()) {
                setWidth(dip2Px(8.0f));
                setHeight(dip2Px(8.0f));
                setPadding(dip2Px(5.0f), dip2Px(5.0f), dip2Px(5.0f), dip2Px(5.0f));
            } else {
                setTextColor(-1);
                setTypeface(Typeface.DEFAULT_BOLD);
                setTextSize(2, 11.0f);
                setPadding(dip2Px(5.0f), dip2Px(5.0f), dip2Px(5.0f), dip2Px(5.0f));
            }
        }
        super.setText(charSequence, bufferType);
    }

    public BadgeView setBadgeCount(int i) {
        setText(String.valueOf(i));
        return this;
    }

    public Integer getBadgeCount() {
        if (getText() == null) {
            return null;
        }
        try {
            return Integer.parseInt(getText().toString());
        } catch (NumberFormatException unused) {
            return null;
        }
    }

    public BadgeView setBadgeGravity(int i) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        layoutParams.gravity = i;
        setLayoutParams(layoutParams);
        return this;
    }

    public int getBadgeGravity() {
        return ((FrameLayout.LayoutParams) getLayoutParams()).gravity;
    }

    public BadgeView setBadgeMargin(int i) {
        return setBadgeMargin(i, i, i, i);
    }

    public BadgeView setBadgeMargin(int i, int i2, int i3, int i4) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        layoutParams.leftMargin = dip2Px(i);
        layoutParams.topMargin = dip2Px(i2);
        layoutParams.rightMargin = dip2Px(i3);
        layoutParams.bottomMargin = dip2Px(i4);
        setLayoutParams(layoutParams);
        return this;
    }

    public int[] getBadgeMargin() {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) getLayoutParams();
        return new int[]{layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin};
    }

    public void incrementBadgeCount(int i) {
        Integer badgeCount = getBadgeCount();
        if (badgeCount == null) {
            setBadgeCount(i);
        } else {
            setBadgeCount(i + badgeCount);
        }
    }

    public void decrementBadgeCount(int i) {
        incrementBadgeCount(-i);
    }

    public void toTargetView(TabWidget tabWidget, int i) {
        toTargetView(tabWidget.getChildTabViewAt(i));
    }

    public void toTargetView(View view) {
        if (getParent() != null) {
            ((ViewGroup) getParent()).removeView(this);
        }
        if (view != null) {
            if (view.getParent() instanceof FrameLayout) {
                ((FrameLayout) view.getParent()).addView(this);
            } else if (view.getParent() instanceof ViewGroup viewGroup) {
                int indexOfChild = viewGroup.indexOfChild(view);
                viewGroup.removeView(view);
                FrameContainer onHideListener = new FrameContainer(getContext())
                        .setBadge(this).setAutoHide(isAutoHide()).setOnHideListener(this.onHideListener);
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                onHideListener.setLayoutParams(layoutParams);
                view.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
                viewGroup.addView(onHideListener, indexOfChild, layoutParams);
                onHideListener.addView(view);
                onHideListener.addView(this);
            } else if (view.getParent() == null){
                Log.e(getClass().getSimpleName(), "ParentView is needed");
            }
        }
    }

    private int dip2Px(float f) {
        return (int) ((f * getContext().getResources().getDisplayMetrics().density) + 0.5f);
    }

    public static String getKey(View view) {
        return "guide_" + view.getClass().getName() + /*BridgeUtil.UNDERLINE_STR*/"_" + view.getId();
    }

    public static boolean shouldShowByFirstTime(View view) {
        return SharedManager.getInstance().getBoolean(getKey(view), true);
    }

    public static void setNotFirst(View view) {
        SharedManager.getInstance().setValue(getKey(view), false);
    }

    public OnHideListener getOnHideListener() {
        return this.onHideListener;
    }

    public BadgeView setOnHideListener(OnHideListener onHideListener) {
        this.onHideListener = onHideListener;
        return this;
    }

    public class FrameContainer extends FrameLayout {
        boolean autoHide;
        View badge;
        private OnHideListener onHideListener;

        public FrameContainer setOnHideListener(OnHideListener onHideListener) {
            this.onHideListener = onHideListener;
            return this;
        }

        public View getBadge() {
            return this.badge;
        }

        public FrameContainer setBadge(View view) {
            this.badge = view;
            return this;
        }

        public boolean isAutoHide() {
            return this.autoHide;
        }

        public FrameContainer setAutoHide(boolean z) {
            this.autoHide = z;
            return this;
        }

        public FrameContainer(Context context) {
            super(context);
            this.autoHide = false;
        }

        public FrameContainer(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            this.autoHide = false;
        }

        public FrameContainer(Context context, AttributeSet attributeSet, int i) {
            super(context, attributeSet, i);
            this.autoHide = false;
        }

        @Override
        public boolean performClick() {
            return super.performClick();
        }

        @Override
        public boolean onTouchEvent(MotionEvent motionEvent) {
            return super.onTouchEvent(motionEvent);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent motionEvent) {
            View view;
            if (motionEvent.getAction() == 0 && this.autoHide && (view = this.badge) != null) {
                removeView(view);
                OnHideListener onHideListener = this.onHideListener;
                if (onHideListener != null) {
                    onHideListener.onHide();
                }
            }
            return super.dispatchTouchEvent(motionEvent);
        }
    }
}
